<?php

namespace app\api\controller\v1;

use app\common\controller\BaseController;
use app\common\validate\TaskValidate;
use app\common\model\Task as TaskModel;

class Task extends BaseController
{
    // 前置操作
    protected $beforeActionList = [
        'isCurrentUser'  =>  ['only'=>'cancel,complete'],
    ];

    // 检测操作的是否为当前用户的数据
    protected function isCurrentUser()
    {
        // 验证
        (new TaskValidate())->goCheck('isCurrentUser');
        $r = (new TaskModel())->isCurrentUser();
        if(!$r) {
            self::interrupt('不可操作他人的数据');
        }
    }

    // 任务详情
    public function index()
    {
        // 验证任务id
        (new TaskValidate())->goCheck('detail');
        $detail = (new TaskModel())->getTaskDetail();
        return self::showResCode('获取成功',['detail'=>$detail]);
    }

    // 发布任务
    public function create()
    {
        // 验证
        (new TaskValidate())->goCheck('create');
        $detail = (new TaskModel())->createTask();
        return self::showResCode('发布成功', compact('detail'));
    }

    // 取消任务
    public function cancel()
    {
        // 验证
        (new TaskValidate())->goCheck('cancel');
        (new TaskModel())->cancel();
        return self::showResCodeWithOutData('取消任务成功');
    }

    // 接任务
    public function receiver()
    {
        (new TaskValidate())->goCheck('receiver');
        $detail = (new TaskModel())->createOrder();
        return self::showResCode('接单成功', compact('detail'));
    }

    // 完成任务
    public function complete()
    {
        // 验证
        (new TaskValidate())->goCheck('complete');
        (new TaskModel())->complete();
        return self::showResCodeWithOutData('操作成功');
    }
}
